package com.ffyc.resale.web;


import com.ffyc.resale.model.Result;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(path = "/CaptchaController/captCtl")
public class CaptchaController {
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/captcha")
    public Result captcha() throws Exception {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();//生成验证码
        String key = UUID.randomUUID().toString();//在后端生成一个随机的key

        Map<String,String> map = new HashMap<>();
        map.put("verKey",key);
        map.put("code",verCode);
        map.put("verCode",specCaptcha.toBase64());//把生成图片验证码,转为base64格式
        // 将key和base64返回给前端
        return new Result(200,"生成成功",map);
    }

}
